package com.pfizer.pfizerExpertForPad;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.pfizer.DB.DataHelper;
import com.pfizer.Entity.pMessage;

/**
 * MessageActivity--<b>訊息</b>
 * 
 * @author krui
 * 
 */
public class MessageActivity extends Activity implements OnClickListener {
	

	// 定義相關變量
	private DataHelper dHelper = null;
	pfizerApplication pApplication = null;
	Context mcontext = null;

	TextView tvMsg = null;
	Button btnOK = null;
	List<pMessage> msgList = null;
	pMessage msg = null;
	int iLength = 0;
	int iCurrItem = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.msgdialog);
		mcontext = MessageActivity.this;
		InitApp initApp = new InitApp(mcontext);
		initApp.SetApp(0);
		pApplication = (pfizerApplication) getApplicationContext();
		dHelper = pApplication.getDataHelper();
		//pApplication.setUserID("A000000003");
		//從數據庫中獲取同步好的訊息
		msgList = dHelper.getMessageByUserID(pApplication.getUserID());
		iLength = msgList.size();//訊息長度
		if (iLength == 0) {
			finish();
			return;
		}
		iCurrItem = 0;
		tvMsg = (TextView) this.findViewById(R.id.tvMsg);
		btnOK = (Button) this.findViewById(R.id.btnOK);
		btnOK.setOnClickListener(this);
		showMessage();
	}

	/**
	 * 顯示訊息
	 */
	private void showMessage() {
		// 得到當前需要顯示的信息
		msg = msgList.get(iCurrItem);
		// 設置顯示
		tvMsg.setText(msg.getCont().trim());
		if (iLength - 1 == iCurrItem) {
			btnOK.setBackgroundResource(R.drawable.btncomplete);
		} else {
			btnOK.setBackgroundResource(R.drawable.btnnext);
		}
	}

	/**
	 * 監聽事件處理
	 */
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		dHelper.updateMessageFlag(msg.getAutoID(), 2);
		if (iLength - 1 == iCurrItem) {
			finish();
		} else {
			iCurrItem++;
			showMessage();
		}
	}
	
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		// TODO Auto-generated method stub
		dHelper = null;
		pApplication = null;
		mcontext = null;
		tvMsg = null;
		btnOK = null;
		msgList = null;
		msg = null;
		
	}
}
